<?php 
/**
 * 默认访问控制器
 */
class ListController extends CommonController{
	/*
	 *默认显示列表页
	 */
	public function index(){
		//调用CommonCortroller里面的topCate方法****
		$this->topCate();
		
		//处理面包屑导航*************
		$cid=$this->findFather();
		//处理面包屑导航下面子分类的部分*******
		$sonCate=M()->query("SELECT * FROM hd_category WHERE pid={$cid}");
		
		//右侧数据处理*********************
		//登陆以后用户的数据处理
		//$userInfo = M()->query("SELECT * FROM hd_user WHERE uid='{$_SESSION['uid']}'");
		//判断SESSION是否存在执行下面的内容
		if(isset($_SESSION['uid']) && isset($_SESSION['uname'])){
			$sql="SELECT * FROM hd_user WHERE uid='{$_SESSION['uid']}'";
			$userData=M()->query($sql);
			if($userData[0]['answer']!=0){
				$userData[0]['cnl']=$userData[0]['accept']/$userData[0]['answer'] *100;
				$userData[0]['cnl']=round($userData[0]['cnl'],2) . "%";
			}else{
				$userData[0]['cnl']=0;
			}
			$userData[0]['lv']=$this->expToLv($userData[0]['exp']);
//			p($userData);
			$this->assign('userData',$userData[0]);
			
		}
		
		// 每日回答问题最多的人*********************
		$todayUid = $this->dayStar();
		$uid = $todayUid[0]['uid'];
		$TodayAns = M()->query("SELECT * FROM hd_user WHERE uid={$uid}");
		$TodayAns[0]['lv'] = $this->expToLv($TodayAns[0]['exp']);
		$this->assign('TodayAns',$TodayAns);
		//p($TodayAns);
		
		//（历史记录）***********
		//接收这个值
		$historyArr=$this->historyStar();
		$historyArr = $historyArr[0];
		//它的经验对应的是经验值
		$historyArr['lv']=$this->expToLv($historyArr['exp']);
		
		$historyArr['accept_l']=$historyArr['accept']/$historyArr['answer']*100 ;
		$historyArr['accept_l']=round($historyArr['accept_l'],2) . '%';
		//p($historyArr);
		//分配出去
		$this->assign('historyArr',$historyArr);
		
			
		//(后盾问答助人光荣榜)*************
		$helpArr = $this->helpPeople();
		$this->assign('helpArr',$helpArr);
		
		//如果没有子集的时候
		if(empty($sonCate)){
			//停留到当前的级
			$pid=M()->query("SELECT pid FROM hd_category WHERE cid={$cid}");
			$pid=$pid[0]['pid'];
			$sonCate=M()->query("SELECT * FROM hd_category WHERE pid={$pid}");
		}
		$this->assign('sonCate',$sonCate);
		
		
		//查询问题（待解决，高悬赏）
		$w=isset($_GET['w']) ? $_GET['w'] : 0;
		switch ($w) {
			case 1:
				$where="solve=1";
				break;
			case 2:
				$where="reward>50";
				break;
			case 3:
				$where="answer=0";
				break;
			//默认为待解决
			default:
				$where="solve=0";
				break;
		}
		//根据不同的where，就可以调取不同的数据
		
		
//		判断cid==0问题表是否为空
		if($cid==0){
			$sql="SELECT * FROM hd_ask as a JOIN hd_category as c ON a.cid=c.cid WHERE {$where}";
		}else{
			$sql="SELECT * FROM hd_ask as a JOIN hd_category as c ON a.cid=c.cid WHERE {$where} AND a.cid={$cid} LIMIT 3";
		}
		$askData=M()->query($sql);
//		p($askData);
		
		$this->assign('askData', $askData);
		
		$this->display();	
	}
	/*
	 * 递归获得父级
	 * */
//	 private function _getFatherCate($data,$cid){
//	 	$temp = array();
//		foreach($data as $v){
//			if($v['cid']==$cid){
//				$temp[]=$v;
//				$temp=array_merge($temp,$this->_getFatherCate($data, $v['pid']));
//			}
//		}
//		return $temp;
//	 }
	
	//**静态变量获取所有的父级
//	private function _getFatherCate($data,$cid){
//		static $temp = array();
//		foreach ($data as $v) {
//			if($v['cid'] == $cid){
//				$temp[] = $v;
//				$this->_getFatherCate($data, $v['pid'])
//			}
//		}
//		return $temp;
//	}
	
	
	
	
	
	
	
}


 ?>